local skels = {}

local skel = fk.CreateSkill {
  name = "rmt__cuoyi",
  tags = {Skill.Compulsory},
}


local DIY = require "packages.diy_utility.diy_utility"


-- 获得转化杀的默认3张选牌，并选择一个目标
---@param player ServerPlayer
local function getDefaultCuoyiData(player)
  local room = player.room
  local cards = player:getCardIds("he")
  if player:getMark("@rmt__zhengjiong") > 0 then
    cards = table.filter(cards, function (id) return Fk:getCardById(id):getMark("@@rmt__zhengjiong-inhand-turn") > 0 end)
  end
  local targets = room:getOtherPlayers(player, false)
  local function check(ids)
    local card = Fk:cloneCard("slash")
    card.skillName = skel.name
    card:addSubcards(ids)
    if player:prohibitUse(card) then return end
    local to = table.find(targets, function (p)
      return player:canUseTo(card, p, {bypass_times = true})
    end)
    if to then
      return {cards = ids, targets = {to}}
    end
  end
  for i, id in ipairs(cards) do
    for j = i+1, #cards do
      for k = j+1, #cards do
        local ret = check{ id, cards[j], cards[k] }
        if ret then
          return ret
        end
      end
    end
  end
  return nil
end


skel:addEffect(fk.EventPhaseStart, {
  name = "rmt__cuoyi",
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase > 2 and player.phase < 7
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local state = DIY.getSwitchState(player, skel.name)
    if state == 1 then
      if #player:getCardIds("he") > 2 and table.find(room:getOtherPlayers(player, false), function (p)
        return player:canUseTo(Fk:cloneCard("slash"), p, {bypass_times = true})
      end) then
        if player:getMark("@rmt__zhengjiong") > 0 and
          #table.filter(player:getCardIds("h"), function (id) --耦
            return Fk:getCardById(id):getMark("@@rmt__zhengjiong-inhand-turn") > 0
          end) < 3 then return end
        local _, dat = room:askToUseActiveSkill(player,{
          skill_name = "rmt__cuoyi_viewas", prompt = "#rmt__cuoyi-slash", cancelable = false,
          extra_data = {bypass_times = true}
        })
        -- 烧条默认值处理
        if not dat then
          dat = getDefaultCuoyiData(player)
        end
        if dat then
          DIY.changeSwitchState(player, skel.name)
          local card = Fk:cloneCard("slash")
          card.skillName = skel.name
          card:addSubcards(dat.cards)
          room:useCard{
            from = player,
            tos = dat.targets,
            card = card,
            extraUse = true,
          }
        end
      end
    elseif state == 2 then
      player:drawCards(3, skel.name)
      DIY.changeSwitchState(player, skel.name)
    elseif state == 3 and player.phase == Player.Play then
      room:addPlayerMark(player, MarkEnum.SlashResidue.."-phase", 3)
      DIY.changeSwitchState(player, skel.name)
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player, skel.name, 1, 3)
end)

skel:addLoseEffect(function (self, player, is_death)
  DIY.removeSwitchSkill(player, skel.name)
end)

Fk:loadTranslationTable{
  ["rmt__cuoyi"] = "厝异",
  [":rmt__cuoyi"] = "转换技，锁定技，你除准备阶段和结束阶段以外的阶段开始时：①你须将三张牌当【杀】使用；②摸三张牌；"..
  "③若为出牌阶段，你此阶段使用【杀】次数上限+3。",
  ["rmt__cuoyi_viewas"] = "厝异",
  ["#rmt__cuoyi-slash"] = "厝异：你须将三张牌当【杀】使用",

  ["$rmt__cuoyi1"] = "同室操戈，胜者王、败者寇。",
  ["$rmt__cuoyi2"] = "兄弟无能，吾当继袁氏大统。",
}

local skel2 = fk.CreateSkill {
  name = "rmt__cuoyi_viewas",
}

skel2:addEffect("viewas", {
  card_filter = function (self, player, to_select, selected)
    if #selected < 3 then
      if player:getMark("@rmt__zhengjiong") == 0 then
        return true
      else -- 为了方便，耦合一下
        return Fk:getCardById(to_select):getMark("@@rmt__zhengjiong-inhand-turn") > 0
      end
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 3 then return end
    local card = Fk:cloneCard("slash")
    card:addSubcards(cards)
    card.skillName = "rmt__cuoyi"
    return card
  end,
})

table.insert(skels, skel)
table.insert(skels, skel2)

return skels
